package com.qf.fmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall.index.entity.Product;
import com.qf.fmall.index.entity.ProductImg;
import com.qf.fmall.index.service.IProductImgService;
import com.qf.fmall.index.service.IProductService;
import com.qf.fmall.product.entity.ProductComments;
import com.qf.fmall.product.entity.ProductParams;
import com.qf.fmall.product.entity.ProductSku;
import com.qf.fmall.product.service.IProductCommentsService;
import com.qf.fmall.product.service.IProductParamsService;
import com.qf.fmall.product.service.IProductSkuService;
import com.qf.fmall.vo.PageVo;
import com.qf.fmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leon
 * @since 2024-01-18
 */
@RestController
@CrossOrigin
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductImgService productImgService;

    @Autowired
    private IProductSkuService productSkuService;

    @Autowired
    private IProductParamsService productParamsService;

    @Autowired
    private IProductCommentsService productCommontsService;




    @RequestMapping("/detail-commontscount/{id}")
    public ResultVo detailCommontscount(@PathVariable("id") Integer id){
        //查询好评数量
//        QueryWrapper<ProductComments> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("product_id",id);
//        queryWrapper.eq("comm_type",1);

        long good = productCommontsService.count(new QueryWrapper<ProductComments>().eq("product_id",id).eq("comm_type",1));
        //查询中评
        long mid = productCommontsService.count(new QueryWrapper<ProductComments>().eq("product_id",id).eq("comm_type",0));
        //查询差评
        long bad = productCommontsService.count(new QueryWrapper<ProductComments>().eq("product_id",id).eq("comm_type",-1));

        //计算好评率

        long total = good+mid+bad;
        String percent = new BigDecimal(good + "").multiply(new BigDecimal("100")).divide(new BigDecimal(total + ""), 2, RoundingMode.HALF_UP).toString();

        HashMap<String, Object> data = new HashMap<>();
        data.put("goodTotal",good);
        data.put("midTotal",mid);
        data.put("badTotal",bad);
        data.put("total",total);
        data.put("percent",percent);

        return new ResultVo(10000, "success", data);

    }






    @RequestMapping("/detail-commonts/{id}")
    public ResultVo detailCommonts(@PathVariable("id") Integer id,Integer pageNum,Integer limit){

        PageVo pageVo =  productCommontsService.datailCommonts(id,pageNum,limit);
        return new ResultVo(10000, "success", pageVo);
    }





    @RequestMapping("/detail-params/{id}")
    public ResultVo detailParams(@PathVariable("id") Integer id){

        ProductParams productParams = productParamsService.getOne(new QueryWrapper<ProductParams>().eq("product_id",id));

        return new ResultVo(10000, "success", productParams);

    }


    @RequestMapping("/detail-info/{id}")
    public ResultVo detailInfo(@PathVariable("id") Integer id) {

        Product product = productService.getById(id);

        List<ProductSku> skus = productSkuService.list(new QueryWrapper<ProductSku>().eq("product_id", id));

        List<ProductImg> productImgs = productImgService.list(new QueryWrapper<ProductImg>().eq("product_id", id));

        HashMap<String, Object> data = new HashMap<>();

        data.put("product", product);
        data.put("productSkus", skus);
        data.put("productImgs", productImgs);

        return new ResultVo(10000, "success", data);
    }
}
